package com.nuoniu.sibanyun.controller.system;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.SysOffice;
import com.nuoniu.sibanyun.entity.dto.SysOfficeDto;
import com.nuoniu.sibanyun.entity.vo.OfficeTreeData;
import com.nuoniu.sibanyun.service.ISysOfficeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 组织机构表 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-05-25
 */
@RestController
@RequestMapping("/system/office")
@Api(value="组织机构 ", tags={"组织机构"})
public class SysOfficeController extends BaseController {

    @Autowired
    private ISysOfficeService sysOfficeService;

    /**
     * 获取机构树列表
     * @author 小啊刚
     * @date 2020/5/27
     */
    @GetMapping("/getOfficeTreeDataList")
    @ApiOperation(value = "获取机构树列表",tags = "APP_组织机构")
    public HttpResult<List<OfficeTreeData>> getOfficeTreeDataList(SysOfficeDto sysOfficeDto) {
        return HttpResult.ok(sysOfficeService.getOfficeTreeDataList(sysOfficeDto));
    }

    /**
     * 获取机构列表
     * @author 小啊刚
     * @date 2020/5/30
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取机构列表")
    public HttpResult getOfficeList(SysOfficeDto sysOfficeDto){
        return HttpResult.ok(sysOfficeService.getOfficeList(sysOfficeDto));
    }

    /**
     * 获取权限部门范围
     * @author baiqiang
     * @date 2021/5/06
     */
    @GetMapping("/getDataScopeOffice")
    @ApiOperation(value = "获取权限部门范围")
    public HttpResult getDataScopeOffice(){
        return HttpResult.ok(sysOfficeService.getDataScopeOffice());
    }

    /**
     * 获取机构列表
     * @author 小啊刚
     * @date 2020/5/30
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页获取机构列表")
    public HttpResult pageList(Page page, SysOfficeDto sysOfficeDto){
        return HttpResult.ok(sysOfficeService.pageList(page, sysOfficeDto));
    }

    /**
     * 机构数据范围
     * @author 小啊刚
     * @date 2020/5/29
     */
    @GetMapping("/checkOfficeList")
    @ApiOperation(value = "获取数据范围 机构id")
    @ApiImplicitParams({
        @ApiImplicitParam(name="type",value="office：机构 user:用户",required=true,paramType="query"),
        @ApiImplicitParam(name="roleId",value="角色ID",required=true,paramType="query"),
    })
    public HttpResult checkOfficeList(Long roleId, String type) {
        return HttpResult.ok(sysOfficeService.checkOfficeList(roleId, type));
    }

    /**
     * 保存机构
     * @author 小啊刚
     * @date 2020/6/1
     */
    @PostMapping("/save")
    public HttpResult saveOffice(@RequestBody SysOffice sysOffice) {
        if(StringUtils.isBlank(sysOffice.getOfficeCode()) || StringUtils.isBlank(sysOffice.getOfficeName())) {
            throw new NuoNiuException("编码和名称不能为空");
        }

        if(sysOffice.getOfficeCode().length() > 18) {
            throw new NuoNiuException("编码不符合规则");
        }

       sysOfficeService.saveOffice(sysOffice);
        return HttpResult.ok();
    }

    /**
     * 机构删除机构
     * @author 小啊刚
     * @date 2020/6/1
     */
    @DeleteMapping("/delete")
    public HttpResult deleteSysOffice(Long id, Long parentId) {
        sysOfficeService.deleteSysOffice(id, parentId);
        return HttpResult.ok();
    }
}
